package com.hengda.museumonline.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.hengda.museumonline.R;
import com.hengda.museumonline.entity.LeaningBean;
import com.hengda.museumonline.ui.ArchContentActivity;
import com.hengda.museumonline.ui.NewsContentActivity;

import static com.yolanda.nohttp.NoHttp.getContext;


/**
 * @author：JFWU on 2016/9/26 18:36
 * @E-mail：jeffy12138@126.com
 */
public class LeaningAdapter extends RecyclerView.Adapter<LeaningAdapter.ListHolder> {
    private Context context;
    private LeaningBean leaningBean;

    public LeaningAdapter(Context context, LeaningBean leaningBean) {
        this.context = context;
        this.leaningBean = leaningBean;
    }

    @Override
    public LeaningAdapter.ListHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.leaning_item, parent, false);
        return new ListHolder(view);
    }

    @Override
    public void onBindViewHolder(LeaningAdapter.ListHolder holder, int position) {
        holder.setData(position);
    }

    @Override
    public int getItemCount() {
        return leaningBean.getData().size();
    }

    class ListHolder extends RecyclerView.ViewHolder {
        private TextView title;
        private TextView date;
        private TextView authorName;
        private TextView hits;
        private CardView leaning_item;

        public ListHolder(View itemView) {
            super(itemView);
            title = (TextView) itemView.findViewById(R.id.title);
            date = (TextView) itemView.findViewById(R.id.date);
            authorName = (TextView) itemView.findViewById(R.id.author_name);
            hits = (TextView) itemView.findViewById(R.id.hits);
            leaning_item = (CardView) itemView.findViewById(R.id.leaning_item);
        }

        public void setData(final int position) {
            title.setText(leaningBean.getData().get(position).getTitle());
            date.setText(leaningBean.getData().get(position).getDate());
            authorName.setText(leaningBean.getData().get(position).getAuthor_name());
            hits.setText(leaningBean.getData().get(position).getHits());
            leaning_item.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Bundle bundle = new Bundle();
                    bundle.putString("id", leaningBean.getData().get(position).getObject_id());
                    Intent intent = new Intent(getContext(), ArchContentActivity.class);
                    intent.putExtras(bundle);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    getContext().startActivity(intent);
                }
            });
        }


    }
}
